package com.niit.user_profile_center.mapper;

import com.niit.user_profile_center.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FollowMapper {

    // 在 FollowMapper.java 中添加以下方法
    @Update("UPDATE tb_follow SET is_deleted = 0, update_date = NOW(), is_friend = 1 WHERE follower_id = #{followerId} AND following_id = #{followingId} AND is_deleted = 1")
    int restoreFollowAndSetFriend(@Param("followerId") Long followerId, @Param("followingId") Long followingId);

    @Insert("INSERT INTO tb_follow (follower_id, following_id, create_date, update_date, is_deleted, version, is_friend) VALUES (#{followerId}, #{followingId}, NOW(), NOW(), 0, 0, 1)")
    int followAndSetFriend(@Param("followerId") Long followerId, @Param("followingId") Long followingId);

    @Update("UPDATE tb_follow SET is_deleted = 1, update_date = NOW(), is_friend = 0 WHERE follower_id = #{followerId} AND following_id = #{followingId} AND is_deleted = 0")
    int unfollowAndClearFriend(@Param("followerId") Long followerId, @Param("followingId") Long followingId);
    @Update("UPDATE tb_follow SET is_friend = #{isFriend} WHERE follower_id = #{followerId} AND following_id = #{followingId}")
    int updateIsFriend(@Param("followerId") Long followerId, @Param("followingId") Long followingId, @Param("isFriend") int isFriend);
    // 新增 Mapper 方法
    @Select("SELECT COUNT(*) FROM tb_follow WHERE follower_id = #{userId} AND following_id = #{otherId} AND is_deleted = 0 AND is_friend = 1")
    int isFriend(@Param("userId") Long userId, @Param("otherId") Long otherId);



    /**
     * 添加关注.
     *
     * @param followerId  粉丝用户ID
     * @param followingId 被关注用户ID
     * @return            插入记录影响的行数
     */
    @Insert("INSERT INTO tb_follow (follower_id, following_id, create_date, update_date, is_deleted, version) VALUES (#{followerId}, #{followingId}, NOW(), NOW(), 0, 0)")
    int follow(@Param("followerId") Long followerId, @Param("followingId") Long followingId);

    /**
     * 检查是否存在已删除的关注记录
     *
     * @param followerId 粉丝用户ID
     * @param followingId 被关注用户ID
     * @return 是否存在已删除的记录
     */
    @Select("SELECT COUNT(*) > 0 FROM tb_follow WHERE follower_id = #{followerId} AND following_id = #{followingId} AND is_deleted = 1")
    Boolean existsDeletedFollow(@Param("followerId") Long followerId, @Param("followingId") Long followingId);
    
    /**
     * 恢复已删除的关注记录
     *
     * @param followerId 粉丝用户ID
     * @param followingId 被关注用户ID
     * @return 更新记录影响的行数
     */
    @Insert("UPDATE tb_follow SET is_deleted = 0, update_date = NOW() WHERE follower_id = #{followerId} AND following_id = #{followingId} AND is_deleted = 1")
    int restoreFollow(@Param("followerId") Long followerId, @Param("followingId") Long followingId);

    /**
     * 取消关注 (逻辑删除).
     *
     * @param followerId 粉丝用户ID
     * @param followingId 被关注用户ID
     * @return            更新记录影响的行数
     */
    @Insert("UPDATE tb_follow SET is_deleted = 1, update_date = NOW() WHERE follower_id = #{followerId} AND following_id = #{followingId} AND is_deleted = 0")
    int unfollow(@Param("followerId") Long followerId, @Param("followingId") Long followingId);

    /**
     * 检查关注状态.
     *
     * @param followerId 粉丝用户ID
     * @param followingId 被关注用户ID
     * @return           返回是否已关注的布尔值
     */
    @Insert("SELECT COUNT(*) > 0 FROM tb_follow WHERE follower_id = #{followerId} AND following_id = #{followingId} AND is_deleted = 0")
    Boolean checkFollowStatus(@Param("followerId") Long followerId, @Param("followingId") Long followingId);

    /**
     * 获取关注列表.
     *
     * @param userId 用户ID
     * @return        返回用户关注的用户列表
     */
    @Select("SELECT u.* FROM tb_follow f LEFT JOIN tb_user u ON f.following_id = u.id WHERE f.follower_id = #{userId} AND f.is_deleted = 0")
    List<User> getFollowingUsers(@Param("userId") Long userId);
    /**
     * 获取粉丝列表.
     *
     * @param userId 用户ID
     * @return        返回用户的粉丝列表
     */
    @Select("SELECT u.* FROM tb_follow f LEFT JOIN tb_user u ON f.follower_id = u.id WHERE f.following_id = #{userId} AND f.is_deleted = 0")
    List<User> getFollowers(@Param("userId") Long userId);

    @Select("SELECT COUNT(*) FROM tb_follow WHERE follower_id = #{userId} AND following_id = #{otherId} AND is_deleted = 0")
    int isFollowing(@Param("userId") Long userId, @Param("otherId") Long otherId);
}
